package com.example.stock.controller;

import com.example.stock.model.DailyGBSta;
import com.example.stock.service.DailyGBStaService;
import com.example.stock.util.GBStaConstant;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("dailyGB")
public class DailyGBStaController {

    @Autowired
    DailyGBStaService dailyGBStaService;

    @GetMapping("20gb")
    public Result get20gb(@RequestParam(required = false) String stock) {
        return ResultGenerator.genSuccessResult(
                builtData(GBStaConstant.DAILY_TYPE_20, stock)
        );
    }

    @GetMapping("30gb")
    public Result get30gb(@RequestParam(required = false) String stock) {
        return ResultGenerator.genSuccessResult(
                builtData(GBStaConstant.DAILY_TYPE_30, stock)
        );
    }

    @GetMapping("60gb")
    public Result get60gb(@RequestParam(required = false) String stock) {

        return ResultGenerator.genSuccessResult(
                builtData(GBStaConstant.DAILY_TYPE_60, stock)
        );
    }

    @GetMapping("100gb")
    public Result get100gb(@RequestParam(required = false) String stock) {

        return ResultGenerator.genSuccessResult(
                builtData(GBStaConstant.DAILY_TYPE_100, stock)
        );
    }

    @GetMapping("300gb")
    public Result get300gb(@RequestParam(required = false) String stock) {

        return ResultGenerator.genSuccessResult(
                builtData(GBStaConstant.DAILY_TYPE_300, stock)
        );
    }

    private List<DailyGBSta> builtData(String type, String stock) {
        List<DailyGBSta> ret;
        if (stock == null) {
            ret = dailyGBStaService.getDailyGBStaListByType(type).stream()
                    .sorted(Comparator.comparing(DailyGBSta::getDate).reversed())
                    .limit(300).toList();

        } else {
            ret = dailyGBStaService.getDailyGBStaListByType(type).stream().filter(i -> i.getStock().equals(stock)).toList();
        }
        return ret;
    }

    @GetMapping("most/{type}/{duration}")
    public Result getByTypeAndDuration(@PathVariable String type, @PathVariable int duration) {
        return ResultGenerator.genSuccessResult(dailyGBStaService.getDataByTypeAndDuration(type, duration));
    }
}